#!/usr/bin/env python
# -*- coding:utf-8 -*-
#

def get_sar_file(data_file):
    
    sys_item = []
    with open(data_file,'r') as f:
        tmp = [ line.replace("AM","").replace("PM","").replace("all","") for line in f.readlines() ]
        #print tmp
        tmp = ''.join(tmp).split('\n')
        #print tmp
        for x in tmp:
            item = x.split()
            if not len(item) ==0:
                sys_item.append(item)
    #print sys_item
    return sys_item

def get_sys_info(sys_file):
    with open(sys_file,'r') as f:
        sys_info = {}
        tmp = [ line.strip() for line in f.readlines() ]
        #print tmp
        for x in tmp:
            item = x.split(',')
            for xx in item:
                it = xx.split(':')
                sys_info[it[0]] = it[1]
    return sys_info

def usage_percent(use, total):
    try:
        usage_percent = (float(use) / total) * 100
    except ZeroDivisionError:
        raise Exception("ERROR - zero division error")
    return usage_percent
#
#    计算list 的最大值、平均值、最小值
#    parameter：int(list)
#        return list:[max,avg,min]
def percent_max_statistic(percent_list):
    percent_max = max(percent_list)
    percent_max = round(percent_max)
    return percent_max

#
#    CPU数用率
#        return cpu_percent：list
# #    sar -u|grep 'all'
# #10:20:02 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
# 10:21:01 AM     all     15.76      0.00      5.19      0.10      0.20     78.74
# 10:22:01 AM     all     14.19      0.00      4.77      0.08      0.18     80.78
# 10:23:01 AM     all     14.84      0.00      4.84      0.12      0.18     80.02
# 10:24:01 AM     all     16.09      0.00      5.04      0.08      0.20     78.59

def cpu_statistic(data_file):
    
    cpu_percent = []
    
    sys_item = get_sar_file(data_file)
    for item in sys_item:
        iowait = float(item[4])
        idle = float(item[6])
        percent = 100 - ( iowait + idle )
        cpu_percent.append(percent)
    #    cpu_percent    [11.149999999999991, 8.120000000000005, 7.640000000000001, 9.030000000000001, 8.14, 8.769999999999996, 8.159999999999997]
    
    return cpu_percent
#cpu_statistic()

#    sar -r |egrep -v 'kbmemfree|Average'
# 12:00:01 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
# 12:01:01 AM   1055748   7119844     87.09    375572   3396996   8624884     53.32   4373920   2098468         0
# 12:02:01 AM   1081696   7093896     86.77    375580   3397068   8388476     51.86   4349700   2098436         0
# 12:03:01 AM   1081612   7093980     86.77    375592   3397116   8397060     51.91   4349208   2098416         0
# 12:04:02 AM   1082912   7092680     86.75    375624   3397164   8398304     51.92   4348852   2098424         0

#
#    内存使用率
#        return
def mem_statistic(data_file):
    
    mem_percent = []
    
    sys_item = get_sar_file(data_file)
    
    for item in sys_item:
        
        kbmemfree = float(item[1])
        kbmemused = float(item[2])
        memused = float(item[3])
        kbbuffers = float(item[4])
        kbcached = float(item[5])
        
        kbtotal = usage_percent(kbmemused, memused)
        
        memused = kbtotal - ( kbmemfree + kbbuffers + kbcached)
        
        percent = usage_percent(memused, kbtotal)
        
        mem_percent.append(percent)
    
    #    mem_percent    [40.93999244365466, 40.62339948598062, 40.624396121782134, 40.6103228962818, 41.04380088951437]
    return mem_percent


def collector():
    # /tmp/cpu_5_12  
    # /tmp/cpu_12_5
    # /tmp/mem_5_12 
    # /tmp/mem_12_5 
    # /tmp/sysinfo
    
    cpu_5_12_file = '/tmp/cpu_5_12'
    cpu_12_5_file = '/tmp/cpu_12_5'
    mem_5_12_file = '/tmp/mem_5_12'
    mem_12_5_file = '/tmp/mem_12_5'
    sys_file = '/tmp/sysinfo'
    
    cpu_5_12_percent = cpu_statistic(cpu_5_12_file)
    cpu_12_5_percent = cpu_statistic(cpu_12_5_file)
    mem_5_12_percent = mem_statistic(mem_5_12_file)
    mem_12_5_percent = mem_statistic(mem_12_5_file)
    
    cpu_5_12_max_percent = percent_max_statistic(cpu_5_12_percent)
    cpu_12_5_max_percent = percent_max_statistic(cpu_12_5_percent)
    mem_5_12_max_percent = percent_max_statistic(mem_5_12_percent)
    mem_12_5_max_percent = percent_max_statistic(mem_12_5_percent)
    
    sys_info = get_sys_info(sys_file)
    
    sys_info['cpu_5_12_max_percent'] = cpu_5_12_max_percent
    sys_info['cpu_12_5_max_percent'] = cpu_12_5_max_percent
    sys_info['mem_5_12_max_percent'] = mem_5_12_max_percent
    sys_info['mem_12_5_max_percent'] = mem_12_5_max_percent

    print sys_info
    
collector()